/*
 * Copyright (c) 2010 Marc Poppleton
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.marcpoppleton.strawberry.provider;

import android.content.Context;
import android.content.res.XmlResourceParser;
import android.util.Log;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

public final class HelpProvider {

    private static final String LOG_TAG = "StrawberryParser";
    private static final String TAG_TOPIC = "topic";
    public static final String TITLE = "title";
    public static final String HELP = "help";


    private HelpProvider() {
    }

    @SuppressWarnings("unchecked")
	public static HashMap<String,List> getHelp(Context context, int resource) {
    	final HashMap<String,List> values = new HashMap<String,List>();
        final XmlResourceParser parser = context.getResources().getXml(resource);

        try {
            inflate(context, parser, values);
        } catch (IOException e) {
            Log.w(LOG_TAG, "An error occured while loading the values", e);
        } catch (XmlPullParserException e) {
            Log.w(LOG_TAG, "An error occured while loading the values", e);            
        } finally {
            parser.close();
        }

        return values;
    }

    @SuppressWarnings("unchecked")
	private static void inflate(Context context, XmlResourceParser parser,HashMap<String,List> content) throws IOException, XmlPullParserException {

        List<Map<String, String>> titleData = new ArrayList<Map<String, String>>();
        List<List<Map<String, String>>> helpData = new ArrayList<List<Map<String, String>>>();
  	
    	int eventType = parser.getEventType();
        List<Map<String, String>> currentChildren = new ArrayList<Map<String, String>>(); 
    	while (eventType != XmlPullParser.END_DOCUMENT) {
            if(eventType == XmlPullParser.START_DOCUMENT) {
                System.out.println("Start document");
            } else if(eventType == XmlPullParser.END_DOCUMENT) {
                System.out.println("End document");
            } else if(eventType == XmlPullParser.START_TAG) {
                if(TAG_TOPIC.equals(parser.getName())){
                    Map<String, String> curGroupMap = new HashMap<String, String>();
                    titleData.add(curGroupMap);
                    curGroupMap.put(TITLE, parser.getAttributeValue(0));
                    currentChildren = new ArrayList<Map<String, String>>();                    
                }
            } else if(eventType == XmlPullParser.END_TAG) {
                if(TAG_TOPIC.equals(parser.getName())){
                	helpData.add(currentChildren);
                }
            } else if(eventType == XmlPullParser.TEXT) {
                Map<String, String> curChildMap = new HashMap<String, String>();
                currentChildren.add(curChildMap);
                curChildMap.put(HELP, parser.getText());                
            }
            eventType = parser.next();
    	}
    	content.put(TITLE, titleData);
    	content.put(HELP,helpData);

    }
}
